﻿using HKB.Common;
using HKB.Web;
using HKB.Web.Admin;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.WebControls;

public partial class Admin_Admin_ShopAddClass : Admin, IRequiresSessionState
{
    protected DefaultProfile profile
    {
        get
        {
            return (DefaultProfile)this.Context.Profile;
        }
    }
    protected global_asax applicationInstance
    {
        get
        {
            return (global_asax)this.Context.ApplicationInstance;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.admin.CheckAdminPower(20))
        {
            this.FormView1.Enabled = false;
        }
        string a = Fetch.Get("Action");
        if (a == "Modify")
        {
            this.FormView1.DefaultMode = FormViewMode.Edit;
            int iD = Fetch.GetID("ClassID");
        }
    }
    protected string GetDisplay()
    {
        string result;
        if (StaticFunApp.ShopConfig["UserTryClothesModel"].IsNull(new object[0]) == "1")
        {
            result = "";
        }
        else
        {
            result = "display:none;";
        }
        return result;
    }
    protected void FormView1_DataBound(object sender, EventArgs e)
    {
        if (Fetch.Get("Action") == "Modify")
        {
            HiddenField hiddenField = (HiddenField)this.FormView1.FindControl("H_ClassParentPath");
            Literal literal = (Literal)this.FormView1.FindControl("BelongClass");
            literal.Text = this.Display_ParentClassName(hiddenField.Value);
            HiddenField hiddenField2 = (HiddenField)this.FormView1.FindControl("H_arrChildID");
            HiddenField hiddenField3 = (HiddenField)this.FormView1.FindControl("H_ParentID");
            string text = hiddenField2.Value;
            string b = base.Request.QueryString["ClassID"];
            if (text == b)
            {
                text = " 1=1";
            }
            else
            {
                string[] array = text.Split(new char[]
				{
					','
				});
                text = string.Join(",", array, 1, array.Length - 1);
                text = " ClassID not in (" + text + ")";
            }
            DataTable dataTable = this.conn.GetDataTable("select ClassName,ClassID,Depth from hlshop_Class where " + text + " order by rootID,OrderByID,Depth asc");
            DropDownList dropDownList = (DropDownList)this.FormView1.FindControl("NewClassArray");
            ListItem item = new ListItem("作为一级分类", "0");
            dropDownList.Items.Add(item);
            Shop_Class shop_Class = new Shop_Class(this.conn);
            shop_Class.BindDropDownListClassMapPath(dropDownList, dataTable, hiddenField3.Value);
        }
        else
        {
            DropDownList dropDownList = (DropDownList)this.FormView1.FindControl("ClassDir");
            Shop_Class shop_Class = new Shop_Class(this.conn);
            shop_Class.BindDropDownListClassMapPath(dropDownList, base.Request["ParentID"]);
        }
    }
    protected void ClassDir_DataBound(object sender, EventArgs e)
    {
        int iD = Fetch.GetID("ParentID");
        DropDownList dropDownList = (DropDownList)this.FormView1.FindControl("ClassDir");
        for (int i = 0; i < dropDownList.Items.Count; i++)
        {
            if (int.Parse(dropDownList.Items[i].Value) == iD)
            {
                dropDownList.Items[i].Selected = true;
                break;
            }
        }
    }
    private string Display_ParentClassName(string ParentPath)
    {
        string text = "";
        if (ParentPath.Contains(","))
        {
            Shop_Class shop_Class = new Shop_Class(this.conn);
            string[] array = ParentPath.Split(new char[]
			{
				','
			});
            for (int i = 1; i < array.Length; i++)
            {
                string classNameByClassID = shop_Class.GetClassNameByClassID(Convert.ToInt32(array[i]));
                string text2 = "&nbsp;&nbsp;&nbsp;";
                for (int j = 1; j < i; j++)
                {
                    text2 += text2;
                }
                if (i < array.Length - 1)
                {
                    string text3 = text;
                    text = string.Concat(new string[]
					{
						text3,
						classNameByClassID,
						"<br>",
						text2,
						"└&nbsp;"
					});
                }
                else
                {
                    text += classNameByClassID;
                }
            }
        }
        else
        {
            text = "无，作为一级栏目";
        }
        return text;
    }
    protected void ObjectDataSource2_Init(object sender, EventArgs e)
    {
        base.Response.Cache.SetNoStore();
    }
    protected void ObjectDataSource2_Updating(object sender, FormViewUpdateEventArgs e)
    {
        CheckBox checkBox = (CheckBox)this.FormView1.FindControl("ChildClassUseSkin");
        if (checkBox.Checked)
        {
            this.ObjectDataSource2.UpdateParameters["ChildClassUseSkin"].DefaultValue = "true";
        }
        else
        {
            this.ObjectDataSource2.UpdateParameters["ChildClassUseSkin"].DefaultValue = "false";
        }
        HiddenField hiddenField = (HiddenField)this.FormView1.FindControl("H_ParentID");
        DropDownList dropDownList = (DropDownList)this.FormView1.FindControl("NewClassArray");
        if (hiddenField.Value != dropDownList.SelectedValue && dropDownList.SelectedValue != base.Request.QueryString["ClassID"])
        {
            this.ChangeClass(base.Request.QueryString["ClassID"], dropDownList.SelectedValue);
        }
    }
    protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
    {
        this.Page.Response.Write("<script>if(!confirm('修改成功,继续修改其他分类吗？')){window.dialogArguments.location.href=window.dialogArguments.location.href;}window.close();</script>");
    }
    protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        this.Page.Response.Write("<script>if(!confirm('添加成功,继续添加其他分类吗？')){window.dialogArguments.location.href=window.dialogArguments.location.href;window.close();}</script>");
    }
    public void ChangeClass(string ClassID, string NewParentID)
    {
        string text = "";
        string sql = "select RootID,Depth, arrChildID,ParentID,OrderByID,Child,parentPath from hlshop_Class where ClassID=" + ClassID + ";";
        Conn conn = this.conn;
        DataTable dataTable = conn.GetDataTable(sql);
        string text2 = "-1";
        if (dataTable != null && dataTable.Rows.Count == 1)
        {
            string text3 = dataTable.Rows[0]["Depth"].ToString();
            string text4 = dataTable.Rows[0]["arrChildID"].ToString();
            string text5 = dataTable.Rows[0]["RootID"].ToString();
            string text6 = dataTable.Rows[0]["ParentID"].ToString();
            string text7 = dataTable.Rows[0]["OrderByID"].ToString();
            string text8 = Convert.ToString((int)dataTable.Rows[0]["Child"] + 1);
            string text9 = dataTable.Rows[0]["parentPath"].ToString();
            text4 = text4.Replace(ClassID, "-1");
            if (NewParentID == "0")
            {
                string text10 = (string)conn.GetScalar("select max(RootID) from hlshop_class", new IDbDataParameter[0]);
                int num = 1;
                if (text10 != null)
                {
                    num = Convert.ToInt32(text10) + 1;
                }
                text = string.Concat(new string[]
				{
					" Update HLShop_Class set ParentID=0,ParentPath=0,Depth=0,RootID=",
					num.ToString(),
					",OrderByID=0 where ClassID=",
					ClassID,
					";",
					Environment.NewLine
				});
                string text11 = text;
                text = string.Concat(new string[]
				{
					text11,
					" Update HLShop_Class set RootID=",
					num.ToString(),
					",Depth=Depth-",
					text3,
					",OrderByID=OrderByID-",
					text7,
					"  where ClassID in(",
					text4,
					");",
					Environment.NewLine
				});
            }
            else
            {
                string sql2 = "select ParentID,arrChildID,Child,Depth,RootID,parentPath,OrderByID from HLShop_Class where ClassID = " + NewParentID;
                DataTable dataTable2 = conn.GetDataTable(sql2);
                if (dataTable2 == null || dataTable2.Rows.Count != 1)
                {
                    return;
                }
                string s = dataTable2.Rows[0]["Depth"].ToString();
                string text12 = dataTable2.Rows[0]["arrChildID"].ToString();
                text2 = dataTable2.Rows[0]["RootID"].ToString();
                string str = dataTable2.Rows[0]["parentPath"].ToString();
                string text13 = dataTable2.Rows[0]["OrderByID"].ToString();
                int num2 = int.Parse(s) + 1;
                string text14 = str + "," + NewParentID;
                int num3 = int.Parse(text13) + 1;
                if (text3 == "0")
                {
                    string sql3 = "select arrChildID from HLSHop_Class Where Depth=0 and RootID!=" + text5 + " Order By RootID asc";
                    DataTable dataTable3 = conn.GetDataTable(sql3);
                    string text11;
                    for (int i = 0; i < dataTable3.Rows.Count; i++)
                    {
                        int num4 = i + 1;
                        text11 = text;
                        text = string.Concat(new string[]
						{
							text11,
							"Update HLShop_Class set RootID=",
							num4.ToString(),
							" where ClassID in (",
							dataTable3.Rows[i]["arrChildID"].ToString(),
							");",
							Environment.NewLine
						});
                    }
                    conn.ExecuteTransaction(text);
                    text = string.Concat(new object[]
					{
						" Update HLShop_Class set RootID=",
						text2,
						",ParentID= ",
						NewParentID,
						",Depth=",
						num2.ToString(),
						",parentPath='",
						text14,
						"',OrderByID=",
						num3,
						" where ClassID=",
						ClassID,
						Environment.NewLine
					});
                    object obj = text;
                    text = string.Concat(new object[]
					{
						obj,
						" Update HLShop_Class set RootID=",
						text2,
						",Depth=Depth+",
						num2,
						" ,OrderByID=OrderByID+",
						num3,
						"  where ClassID in(",
						text4,
						");",
						Environment.NewLine
					});
                    text11 = text;
                    text = string.Concat(new string[]
					{
						text11,
						" Update HLShop_Class set OrderByID=OrderByID+",
						text8,
						" where OrderByID>",
						text13,
						" and RootID=",
						text2,
						" and ClassID not in(",
						ClassID,
						",",
						text4,
						");"
					});
                }
                else
                {
                    string[] array = new string[]
					{
						"+",
						"-"
					};
                    int num5 = num3 - int.Parse(text7);
                    int value = num2 - int.Parse(text3);
                    if (num5 < 0)
                    {
                        array = new string[]
						{
							"-",
							"+"
						};
                    }
                    text = string.Concat(new object[]
					{
						" Update HLShop_Class set RootID=",
						text2,
						",ParentID=",
						NewParentID,
						",Depth=",
						num2.ToString(),
						",parentPath='",
						text14,
						"',OrderByID=",
						num3,
						" where ClassID=",
						ClassID,
						Environment.NewLine
					});
                    if (int.Parse(text8) > 1)
                    {
                        object obj = text;
                        text = string.Concat(new object[]
						{
							obj,
							" Update HLShop_Class set RootID=",
							text2,
							",Depth=Depth",
							array[0],
							Math.Abs(value),
							",OrderByID=OrderByID",
							array[0],
							Math.Abs(num5),
							" where  ClassID in(",
							text4,
							");",
							Environment.NewLine
						});
                    }
                    string text11 = text;
                    text = string.Concat(new string[]
					{
						text11,
						" Update HLShop_Class set OrderByID=OrderByID+",
						text8,
						" where OrderByID>",
						text13,
						" and RootID=",
						text2,
						" and ClassID not in(",
						ClassID,
						",",
						text4,
						");",
						Environment.NewLine
					});
                }
            }
            conn.ExecuteTransaction(text);
            Shop_Class shop_Class = new Shop_Class();
            if (text3 != "0")
            {
                if (text2 != text5)
                {
                    shop_Class.RepairClass();
                    shop_Class.SortRootChildClass(int.Parse(text5));
                }
            }
            if (NewParentID != "0")
            {
                shop_Class.RepairClass();
                shop_Class.SortRootChildClass(int.Parse(text2));
            }
        }
    }
    protected void ProductSkinID_OnDataBinding(object sender, EventArgs e)
    {
    }
}
